home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 280_01 / msort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-11  |  4.6 KB  |  232 lines

  1. /* [msort.c of JUGPDS Vol.46] */
  2. /*
  3. *****************************************************************
  4. *                                *
  5. *    Written by  Hakuo Katayose (JUG-CP/M No.179)        *
  6. *            49-114 Kawauchi-Sanjuunin-machi        *
  7. *            Sendai, Miyagi 980                          *
  8. *            Phone: 0222-61-3219                *
  9. *                                *
  10. *       Modifird by Toshiya Oota   (JUG-CPM No.10)              *
  11. *                   Sakae ko-po 205                 *
  12. *            5-19-6 Hosoda                *
  13. *            Katusikaku Tokyo 124            *
  14. *                                *
  15. *        for MS-DOS Lattice C V3.1J & 80186/V20/V30    *
  16. *                                *
  17. *    Compiler Option: -ccu -k0(1) -ms -n -v -w        *
  18. *                                *
  19. *    Edited & tested by Y. Monma (JUG-CP/M Disk Editor)    *
  20. *            &  T. Ota   (JUG-CP/M Sub Disk Editor)    *
  21. *                                *
  22. *****************************************************************
  23. */
  24.  
  25. /* Library functions for Software Tools */
  26.  
  27. #include "stdio.h"
  28. #include "dos.h"
  29. #include "ctype.h"
  30. #include "tools.h"
  31. #include "toolfunc.h"
  32.  
  33. #define    UBUFSIZE    64
  34.  
  35.  
  36. /* msort - sort of text (ASCII) file 
  37.  
  38.     Usage: msort -{dfnlr} <infile >outfile ^Z");
  39. where
  40.     -d: dictionary order 
  41.     -f: fold order
  42.     -n: numeric order
  43.     -l: line # listing
  44.     -r: reverse (decreasing) order 
  45.  
  46. */
  47.  
  48. #define LINES    1000                    /* Modify 1986-11-28 */
  49. #define MAXLEN    128
  50. #define MAXBUF    30
  51.  
  52. char    allocbuf[LINES][MAXBUF], *alloc_bp, *alloc_ep;
  53.  
  54. char    opt_d;        /* dictionary order */
  55. char    opt_f;        /* fold order        */
  56. char    opt_n;        /* numeric order    */
  57. char    opt_l;        /* line_no listing  */
  58. char    opt_r;        /* reverse order    */
  59.  
  60. int    debug;                    /* debug switch */
  61.  
  62. void main(argc, argv)
  63. int argc;
  64. char *argv[];
  65.  
  66. {
  67. char    *ap;
  68. int    i;
  69. void    filesort();
  70.  
  71.     if (argc < 1) {
  72.         error("MSO901 Usage: msort -{dfnlr} <infile >outfile");
  73.         exit(1);
  74.         }
  75.     else
  76.     opt_d = opt_f = opt_n = opt_l = opt_r = OFF;
  77.     debug = OFF;
  78.     i = 0;
  79.     while (--argc > 0 && (*++argv)[0] == '-')
  80.         for (ap = argv[0]+1; *ap != '\0'; ap++)
  81.             switch (tolower(*ap)) {
  82.             case 'd':
  83.                 opt_d = ON;
  84.                 break;
  85.             case 'f':
  86.                 opt_f = ON;
  87.                 break;
  88.             case 'n':
  89.                 opt_n = ON;
  90.                 break;
  91.             case 'l' :
  92.                 opt_l = ON;
  93.                 break;
  94.             case 'r':
  95.                 opt_r = ON;
  96.                 break;
  97.             case 'x':
  98.                 debug = ON;
  99.                 break;
  100.             }
  101.     if(debug) {
  102.       fprintf(STDERR,"\nMSO801 debug data for msort\n       ");
  103.       fprintf(STDERR,"opt_d %d,opt_f %d,opt_n %d,opt_l %d,opt_r %d",
  104.                 opt_d,opt_f,opt_n,opt_l,opt_r);
  105.     }
  106.     filesort();
  107. }
  108.  
  109. void filesort()
  110. {
  111. char    *lineptr[LINES];
  112. int    nlines, high;
  113. void    sort(),ptext();
  114.  
  115.     high = 0;
  116.     if (gtext(lineptr, &nlines, LINES, STDIN) == EOF) {
  117.         sort (lineptr, &nlines);
  118.         ptext(lineptr, &nlines, STDOUT);
  119.         }
  120.     else
  121.         fprintf(STDERR, "MSO902 Input too big to sort\n" );
  122. }
  123.  
  124.  
  125. void sort(lineptr, nlines)
  126. char *lineptr[];
  127. int  *nlines;
  128. {
  129. void quick(),swap();
  130. int    strdfcmp(), strfcmp(), strcmp(), strdcmp(), numcmp();
  131.  
  132.     if (opt_n == ON)
  133.         quick(lineptr, 0, (*nlines)-1, numcmp, swap);
  134.     else if (opt_d == ON && opt_f == ON)
  135.         quick(lineptr, 0, (*nlines)-1, strdfcmp, swap);
  136.     else if (opt_d == ON)
  137.         quick(lineptr, 0, (*nlines)-1, strdcmp, swap);
  138.     else if (opt_f == ON)
  139.         quick(lineptr, 0, (*nlines)-1, strfcmp, swap);
  140.     else
  141.         quick(lineptr, 0, (*nlines)-1, strcmp, swap);
  142. }
  143.  
  144.  
  145. int  gtext(lineptr, nlines, maxlines, fp)
  146. char    *lineptr[];
  147. int    *nlines;
  148. int    maxlines;
  149. FILE    *fp;
  150. {
  151. int    len;
  152.  
  153.     *nlines = 0;
  154.     alloc_bp = &allocbuf[0][0];
  155.     alloc_ep = &allocbuf[(LINES-1)][(MAXBUF-1)];
  156.     do {
  157.         if ((len = fgetlin(fp, alloc_bp, MAXLEN)) <= 0)
  158.             return EOF;
  159.         lineptr[(*nlines)++] = alloc_bp;
  160.         alloc_bp += len;
  161.         *(alloc_bp - 1) = '\0';
  162.         } while (alloc_bp + MAXLEN <= alloc_ep && *nlines < maxlines);
  163.     return(len);
  164. }
  165.  
  166. void ptext(lineptr, nlines, fp)
  167. char    *lineptr[];
  168. int    *nlines;
  169. FILE    *fp;
  170. {
  171. int    lno;
  172.  
  173.     lno = 1;
  174.     if( opt_r == ON ) lineptr += (*nlines);
  175.     while ((*nlines)--) {
  176.         if( opt_l == ON )
  177.             fprintf(fp, "%6u: ", lno++);
  178.         fprintf(fp, "%s\n", ( opt_r == ON ? *--lineptr : *lineptr++) );
  179.         }
  180. }
  181.  
  182. int  numcmp(s1, s2)
  183. char *s1, *s2;
  184. {
  185.     int    atoi(), v1, v2;
  186.  
  187.     v1 = atoi(s1);
  188.     v2 = atoi(s2);
  189.     if(v1 < v2)
  190.         return(-1);
  191.     else if(v1 > v2)
  192.         return(1);
  193.     else
  194.         return(0);
  195. }
  196.  
  197. void quick(v, l, r, comp, exch)
  198. char    *v[];
  199. int    l, r;
  200. int    (*comp)(), (*exch)();
  201. {
  202. char    *vx;
  203. int    i, j;
  204.  
  205.     i = l;  j = r;
  206.     vx = v[ (l+r)/2 ];
  207.     while(i <= j) {
  208.         while( (*comp)(v[i], vx) < 0 )
  209.             i++;
  210.         while( (*comp)(vx, v[j]) < 0 )
  211.             j--;
  212.         if(i <= j) {
  213.             (*exch)(&v[j], &v[i]);
  214.             i++;
  215.             j--;
  216.             }
  217.         }
  218.     if(l < j) quick(v,l,j,comp,exch);
  219.     if(i < r) quick(v,i,r,comp,exch);
  220. }
  221.  
  222.  
  223. void swap(px, py)
  224. char    *px[], *py[];
  225. {
  226. char    *temp;
  227.  
  228.     temp = *px;
  229.     *px = *py;
  230.     *py = temp;
  231. }
  232.